package com.javaspeak.algorithm.prime.sieveoferatosthenes;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;

/**
 * @author John Dickerson
 */
public class TestCreatePrimeNumbers {

    public Logger logger = Logger.getLogger( TestCreatePrimeNumbers.class );

    private List<Long> getExpectedPrimeNumbers( long number ){

        List<Long> primeNumbers = new ArrayList<Long>();

        boolean isPrime = false;

        for ( long i=2; i<=number; i++ ){

            isPrime = true;

            for ( long j=2; j<i; j++ ){

                if ( i % j == 0 ){

                    isPrime = false;
                    break;
                }
            }

            if ( isPrime ){

                primeNumbers.add( new Long( i ) );
            }
        }

        return primeNumbers;
    }


    /**
     * We are testing creating prime numbers using "Sieve of Eratosthenes"
     * algorithm.
     */
    @Test
    public void testCreatePrimes(){

        int number = 37;

        List<Long> expectedPrimeNumbers = getExpectedPrimeNumbers( number );

        StringBuilder expected = new StringBuilder( "Expected : " );
        StringBuilder actual = new StringBuilder( "Actual   : " );

        for ( Long primeNumber : expectedPrimeNumbers ){

            expected.append( primeNumber ).append( " " );
        }

        List<Long> actualNumbers =
            CreatePrimeNumbers.createPrimeNumbers( number );

        for ( Long primeNumber : expectedPrimeNumbers ){

            actual.append( primeNumber ).append( " " );
        }

        logger.debug( expected.toString() );
        logger.debug( actual.toString() );

        org.junit.Assert.assertArrayEquals(
                expectedPrimeNumbers.toArray( new Long[]{} ),
                    actualNumbers.toArray( new Long[]{} ) );
    }


    public static void main( String args[] ){

        BasicConfigurator.resetConfiguration();
        BasicConfigurator.configure();

        JUnitCore junitCore = new JUnitCore();
        junitCore.addListener( new TextListener( System.out ) );
        junitCore.run( TestCreatePrimeNumbers.class );
    }
}
